package com.lagou.user.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lagou.entity.User;
import com.lagou.entity.DTO;
import com.lagou.entity.UserVo;
import com.lagou.entity.FileSystem;
import com.lagou.user.UserService;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@RequestMapping("user")
public class UserController {

    @Reference // 远程消费
    private UserService userService;

    @GetMapping("login")
    public DTO login(String phone, String password, String nickname, String headimg) {
        DTO dto = new DTO();
        User user = null;
        System.out.println("phone = " + phone);
        System.out.println("password = " + password);
        System.out.println("nickname = " + nickname);

        // 检测手机号是否注册
        Integer i = userService.checkPhone(phone);
        if(i == 0){
            // 未注册，自动注册并登录
            userService.register(phone, password,nickname,headimg);
            dto.setMessage("手机号尚未注册，系统已帮您自动注册，请牢记密码！");
            user = userService.login(phone, password);
        }else{
            user = userService.login(phone, password);
            if(user == null){
                dto.setState(300); //300表示失败
                dto.setMessage("帐号密码不匹配，登录失败！");
            }else{
                dto.setState(200); //200表示成功
                dto.setMessage("登录成功！");
            }
        }

        dto.setContent(user);
        return dto;
    }

    @PostMapping("upload")
    public FileSystem upload(@RequestParam("file")MultipartFile file) throws IOException {

        System.out.println("接收到：" +file);

        //判断文件是否为空
        if(file.isEmpty()){
            throw new RuntimeException();
        }

        FileSystem fs = new FileSystem();

        //获得文件的原始名称
        String oldFileName = file.getOriginalFilename();
        //获得后缀名
        String suffix = oldFileName.substring(oldFileName.lastIndexOf(".")+1);

        try {
            //加载配置文件
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            //创建tracker客户端
            TrackerClient tc = new TrackerClient();
            //根据tracker客户端创建连接
            TrackerServer ts = tc.getConnection();
            StorageServer ss = null;
            //定义storage客户端
            StorageClient1 client = new StorageClient1(ts, ss);
            //文件元信息
            NameValuePair[] list = new NameValuePair[1];
            list[0] = new NameValuePair("fileName", oldFileName);
            //上传，返回fileId
            String fileId = client.upload_file1(file.getBytes(), suffix, list);
            System.out.println(fileId);
            ts.close();
            //封装数据对象，将路径保存到数据库（本次不写）
            fs.setFileId(fileId);
            fs.setFilePath(fileId);
            fs.setFileName(oldFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fs;
    }

    @RequestMapping("updateUserInfo")
    public DTO updateUserInfo(@RequestBody UserVo userVo) {
        Integer i = userService.updateUserInfo(userVo.getUserid(), userVo.getPortrait(), userVo.getName());
        if (i > 0)
            return new DTO(true, 200, "修改成功", null);
        else
            return new DTO(false, 300, "修改失败", null);
    }

    @RequestMapping("updatePassword")
    public DTO updatePassword(@RequestBody UserVo userVo) {
        Integer i = userService.updatePassword(userVo.getUserid(), userVo.getPassword());
        if (i > 0)
            return new DTO(true, 200, "修改成功", null);
        else
            return new DTO(false, 300, "修改失败", null);
    }
}
